package com.mall.shopping.services;

import com.mall.commons.tool.exception.ValidateException;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dto.AllProductCateRequest;
import com.mall.shopping.dto.AllProductCateResponse;
import com.mall.shopping.dto.ProductCateDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author ShuiYi
 * @time 2021-07-22 10:44
 */
@Slf4j
@Service
public class IProductCateServiceImpl implements IProductCateService {
    @Autowired
    private ItemCatMapper itemCatMapper;
    @Autowired
    private ProductCateConverter productCateConverter;

    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {

        AllProductCateResponse allProductCateResponse = new AllProductCateResponse();
        try {

            // request.requestCheck();

            Example exampleItemCat = new Example(ItemCat.class);
            if (request.getSort() != null) {
                exampleItemCat.setOrderByClause("sort_order" + "\t" + request.getSort());
            }


            List<ItemCat> itemCats = itemCatMapper.selectByExample(exampleItemCat);

            List<ProductCateDto> productCateDtos = productCateConverter.items2Dto(itemCats);
            allProductCateResponse.setProductCateDtoList(productCateDtos);
            allProductCateResponse.setCode(ShoppingRetCode.SHOPPING_HOMEPAGE.getCode());
            allProductCateResponse.setMsg(ShoppingRetCode.SHOPPING_HOMEPAGE.getMessage());

            return allProductCateResponse;
        } catch (ValidateException v) {
            allProductCateResponse.setCode(ShoppingRetCode.PARAMETER_VALIDATION_FAILED.getCode());
            allProductCateResponse.setMsg(ShoppingRetCode.PARAMETER_VALIDATION_FAILED.getMessage());
        } catch (Exception e) {
            allProductCateResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            allProductCateResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return allProductCateResponse;
    }
}
